{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Design 100 qubits programmatically"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Prerequisite\n",
    "A working local installation of Ansys"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## N_x by N_y grid of qubits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A simple example of generating a large grid of qubits.\n",
    "\n",
    "Using TransmonPocket and RouteMeander, we can generate a large, varied grid array of qubits. This is not a practical design given the lack of readouts or control lines to the qubits, just an example for how to make a design algorithmically."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import qiskit_metal as metal\n",
    "from qiskit_metal import designs, MetalGUI\n",
    "from qiskit_metal import Dict, Headings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "design = designs.DesignPlanar()\n",
    "gui = MetalGUI(design)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit_metal.qlibrary.qubits.transmon_pocket import TransmonPocket\n",
    "from qiskit_metal.qlibrary.tlines.meandered import RouteMeander"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "design.overwrite_enabled = True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Design"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can vary how big we want the grid to be by changing N_x/N_y (number of qubits along the x/y axis).\n",
    "Be careful as very large arrays can take a fair bit of time to generate. We modify the chip size so it contains all of the qubits."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "N_x =4\n",
    "N_y =3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "design.chips.main.size.size_x = str((N_x+1) * 3)+'mm'\n",
    "design.chips.main.size.size_y = str((N_y+1) * 3)+'mm'\n",
    "\n",
    "design.chips.main.size.center_x = str((N_x-1) * 1.5)+'mm'\n",
    "design.chips.main.size.center_y = str((N_y-0.5) * 1.5)+'mm'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First we generate the qubits. We use some simple math to generate the offset pattern in order to make the bus resonators easier to connect."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Loop to generate and draw the qubits\n",
    "for x in range(N_x):\n",
    "    for y in range(N_y):\n",
    "        options = dict(pos_x= str(x*3000)+'um', pos_y = str(y*3000 + (x%2)*1500)+'um', orientation = \"-90\",\n",
    "                      connection_pads = dict(\n",
    "                            B0 = dict(loc_W=-1, loc_H=-1, pad_width='75um'),\n",
    "                            B1  = dict(loc_W=-1, loc_H=+1, pad_width='120um'),\n",
    "                            B2  = dict(loc_W=+1, loc_H=-1, pad_width='120um'),\n",
    "                            B3 = dict(loc_w = +1, loc_H = +1, pad_width='90um')))\n",
    "\n",
    "        \n",
    "        obj=TransmonPocket(design,'Q_'+str(x)+'_'+str(y),options)\n",
    "        \n",
    "        \n",
    "gui.rebuild()\n",
    "gui.autoscale()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we generate the route meanders. As we used consistent naming schemes for the qubits and pins, we can loop through them with out issue.\n",
    "We also have the length vary based on which qubit is being connected, such that no qubit should be connected to two resonators of the same frequency."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for x in range(N_x):\n",
    "    for y in range(N_y):\n",
    "        #\"upward\" connection, avoids drawing connectors for 'top' row. Changes connector length by +/-50um to avoid frequency collisions\n",
    "        if y<(N_y-1):\n",
    "            connectorAD = RouteMeander(design,'CU_'+str(x)+'_'+str(y),options = dict(total_length = str(7+(y%2)*0.5)+'mm',\n",
    "                                                                                    fillet = \"99um\",lead=dict(\n",
    "                                                                                    start_straight='0.5mm',\n",
    "                                                                                    end_straight='0.25mm'),\n",
    "                                                                                    meander = dict(asymmetry='-700um'),\n",
    "                                                                                    pin_inputs = dict(\n",
    "                                                                                        start_pin=dict(\n",
    "                                                                                            component ='Q_'+str(x)+'_'+str(y),\n",
    "                                                                                            pin = 'B0'), \n",
    "                                                                                        end_pin=dict(\n",
    "                                                                                            component='Q_'+str(x)+'_'+str(y+1),\n",
    "                                                                                            pin='B3'))))\n",
    "            \n",
    "        #\"sideways\" connection, avoids drawing for far right col, and for top qubit in odd col. Changes connector length by +/- 25um \n",
    "        #to avoid frequency collisions\n",
    "        if x<(N_x-1) and (not(x%2 and y==(N_y-1))):\n",
    "            connectorBC = RouteMeander(design,'CS_'+str(x)+'_'+str(y),options= dict(total_length = str(6+(y%2)*0.5)+'mm',\n",
    "                                                                                    fillet = \"99um\",lead=Dict(\n",
    "                                                                                        start_straight='0.3mm',\n",
    "                                                                                        end_straight='0.25mm'),\n",
    "                                                                                    meander = Dict(asymmetry='-200um'),\n",
    "                                                                                    pin_inputs = Dict(\n",
    "                                                                                        start_pin=Dict(\n",
    "                                                                                            component ='Q_'+str(x)+'_'+str(y),\n",
    "                                                                                            pin = 'B1'), \n",
    "                                                                                        end_pin=Dict(\n",
    "                                                                                            component='Q_'+str(x+1)+'_'+str(y+(x%2)),\n",
    "                                                                                             pin='B2'))))\n",
    "            \n",
    "\n",
    "gui.rebuild()\n",
    "gui.autoscale()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "nbsphinx-thumbnail"
    ]
   },
   "outputs": [],
   "source": [
    "gui.screenshot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Analysis"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can quickly check if we have designed the qubits well with an LOM analysis. \n",
    "First we select the design that we want to analyse and the tool to use for any simulation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit_metal.analyses.quantization import LOManalysis\n",
    "c1 = LOManalysis(design, \"q3d\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can review and update the Analysis default setup following the examples in the next two cells."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c1.sim.setup"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's change the name of the setup and increase the maximum number of passes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c1.sim.setup.name = 'LOM'\n",
    "c1.sim.setup.max_passes = 14\n",
    "\n",
    "#To change multiple settings use the following method:\n",
    "#c1.sim.setup_update(name = 'LOM', max_passes = 14)\n",
    "\n",
    "c1.sim.setup\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For our analysis we will pick the first qubit, Q_0_0, making sure to apply open terminations to the connection pads. The default bounding box is sufficient for a simple analysis. By passing the parameter `components` to the `sim.run()` method, the design is rendered automatically. Not passing it skips the rendering and tries to run the analysis on the latest design. If a design is not found, the full metal design is rendered."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c1.sim.run(components=['Q_0_0'], open_terminations=[('Q_0_0', 'B0'), ('Q_0_0', 'B1'), ('Q_0_0', 'B2'), ('Q_0_0', 'B3')])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We then use the results of that simulation to complete a LOM analysis. The input values should be modified based on the simulation passes and the set frequencies of the resonators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c1.setup.junctions = Dict({'Lj': 12.31, 'Cj': 2})\n",
    "c1.setup.freq_readout = 6.6\n",
    "c1.setup.freq_bus = [6.0, 6.2,6.4]\n",
    "\n",
    "c1.run_lom()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once you are done with your analysis, please close it with `close()`. This will free up resources currently occupied by qiskit-metal to communiate with the tool."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c1.sim.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## GDS Render"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "chip_gds = design.renderers.gds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "chip_gds.options['no_cheese']['buffer'] = '50um'\n",
    "chip_gds.options['path_filename'] = '../../resources/Fake_Junctions.GDS'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "chip_gds.export_to_gds(\"NxN_Chip.gds\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can close the GUI now that we have finished the design."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gui.main_window.close()"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
